﻿//===============================================================================
// Microsoft patterns & practices
// CompositeUI Application Block
//===============================================================================
// Copyright © Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================

using System;
using System.Runtime.Serialization;
using BusinessEntity.Common;
using DevExpress.XtraEditors.DXErrorProvider;

namespace BusinessEntity.Patient
{
    [DataContract]
    public class Patient : IDXDataErrorInfo
    {
        [DataMember]
        public int? Id { get; set; }
        
        [DataMember]
        public string FirstName { get; set; }
        [DataMember]
        public string MiddleName { get; set; }
        [DataMember]
        public string LastName { get; set; }
        [DataMember]
        public string DisplayName { get; set; }
       
        [DataMember]
        public DateTime? MostRecentAppointment { get; set; }
        [DataMember]
        public int? ActiveVisitId { get; set; }
        [DataMember]
        public SortableCollection<PatientVisit> PatientVisit { get; set; }
        
        [DataMember]
        public DateTime? VisitStartDate { get; set; }
        [DataMember]
        public DateTime? VisitEndDate { get; set; }
        [DataMember]
        public PatientVisit ActiveVisit
        {
            get
            {
                if (PatientVisit == null)
                {
                    PatientVisit oVisit = new PatientVisit();
                    oVisit.PatientAppointment = new PatientAppointment();
                    oVisit.PatientAppointment.StartDate = System.DateTime.Now;
                    return oVisit;
                }

                for (int i = 0; i < PatientVisit.Count; i++)
                {
                    if (PatientVisit[i].Id == ActiveVisitId)
                        return PatientVisit[i];
                }
                return null;
            }
            set
            {
                //throw new NotImplementedException("Use ActiveVisitId property to set the active visit");
            }
        }
        [DataMember]
        public DateTime? ActiveVisitDate
        {
            get
            {
                if (ActiveVisit == null)
                    return null;

                return ActiveVisit.VisitDate;
            }
            set
            {
                //throw new NotImplementedException("Use ActiveVisitId property to set the active visit");
            }
        }
        [DataMember]
        public DateTime? LastVisitDate { get; set; }
        [DataMember]
        public Log Log { get; set; }


        #region IDXDataErrorInfo Members

        void IDXDataErrorInfo.GetError(ErrorInfo info)
        {
        }

        void IDXDataErrorInfo.GetPropertyError(string propertyName, ErrorInfo info)
        {
            if (propertyName == "FirstName" && FirstName == "" ||
                propertyName == "LastName" && LastName == "")
            {
                info.ErrorText = String.Format("The '{0}' field cannot be empty", propertyName);
            }
        }
        #endregion
    }
}